package cn.kgc.viny.smbms.web;

import cn.kgc.viny.smbms.entity.User;
import cn.kgc.viny.smbms.service.Constant;
import cn.kgc.viny.smbms.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginOutController {
    @Resource
    private UserService userService;
    //前往登录页
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }
    //登录
    @RequestMapping("/login")
    public String login(String userCode, String userPassword, HttpSession session){
        User user =this.userService.findUser(userCode,userPassword);
        if(user!=null){//登录成功
            session.setAttribute(Constant.USER,user);
            //跳转：重定向
            return "redirect:toFrame";
        }
        //登录失败
        return "redirect:toLogin?flag=-1";
    }
    //前往工作平台
    @RequestMapping("/toFrame")
    public String toFrame(){
        return "frame";
    }
    //退出
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:toLogin";
    }
}
